<template>
  <div>
    <el-form :model="linkForm" :rules="rules" ref="linkForm" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="linkForm.name"></el-input>
      </el-form-item>
      <el-form-item label="链接地址" prop="linkUrl">
        <el-input v-model="linkForm.linkUrl"></el-input>
      </el-form-item>
      <el-form-item label="图标地址" prop="imgUrl">
        <el-input v-model="linkForm.imgUrl"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="linkForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="linkSubmit">确认添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      linkForm: {
        name: "",
        linkUrl: "http://baidu.com",
        imgUrl: "http://localhost:4000/images/default/0.jpg",
        desc: "风华绝帽",
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        linkUrl: [
          {
            required: true,
            type: "url",
            message: "你输入的内容不是一个url",
            trigger: "blur",
          },
        ],
        imgUrl: [
          {
            required: true,
            type: "url",
            message: "你输入的内容不是一个url",
            trigger: "blur",
          },
        ],
        desc: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations(["changeLink"]),
    async linkSubmit() {
      this.$refs.linkForm.validate(async (v) => {
        if (v) {
          let { data } = await this.$axios({
            method: "POST",
            url: "/administer/link/addData",
            data: this.linkForm,
          });
          if (data.code) return this.$message.error(data.message);
          this.$message.success(data.message);
          this.$refs.linkForm.resetFields();
          this.changeLink(); //修改vuex里的state数据
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-form-item {
  margin: 40px 20px;
  margin-left: 0;
}
</style>